var bAmount;

var http = getHttpObject();
/* 确保在ie的各式版本，以及在firefox或其他浏览器下，都有用的xmlhttpRequest  */
function getHttpObject() {
	var xmlhttp = false;
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
		if (xmlhttp.overrideMimeType) {
			xmlhttp.overrideMimeType("text/xml");
		}
	} else {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}



function handleHttpResponseOfCost() {
	if (http.readyState == 4) {
		if (http.status == 200) {
			var text_array = http.responseText.split(",");
			//alert(text_array[0]);
			document.getElementById("cost" + text_array[0]).value = text_array[1];			
		} else {
			alert("error!!!");
			alert(http.status);
		}
	}
}
	
	function getCostInfo(index){	
			var measureUnitName = document.getElementById("measureUnitName"+index).value;		
			var basicCost = document.getElementById("basicCost"+index).value;		
			
			var url = "${pageContext.request.contextPath}/getCost.action?measureUnitName="+measureUnitName+"&rowIndex="+index+"&basicCost="+basicCost;
			
			
			http.open("POST", url, true);
			http.onreadystatechange = handleHttpResponseOfCost;
			http.send(null);
			return;
		
}


function handleHttpResponseOfBasicCost() {
	if (http.readyState == 4) {
		if (http.status == 200) {
			var text_array = http.responseText.split(",");
			//alert(text_array[0]);
			document.getElementById("basicCost" + text_array[0]).value = text_array[1];	
					
		} else {
			alert("error!!!");
			alert(http.status);
		}
	}
}

function getBasicCostInfo(index){
			var measureUnitName = document.getElementById("measureUnitId"+index).value;		
			var cost = document.getElementById("cost"+index).value;		
		
			var url = "${pageContext.request.contextPath}/getBasicCost.action?measureUnitName="+measureUnitName+"&rowIndex="+index+"&cost="+cost;
			
			http.open("POST", url, true);
			http.onreadystatechange = handleHttpResponseOfBasicCost;
			http.send(null);
			return;
}

/* 采购入库改变数量时，基本单位数量做相应修改 */
function getBasicAmount(index, measureUnitId, amount, basicAmount) {
	
	bAmount = basicAmount;
	var measureUnitId = document.getElementById(measureUnitId + index).value;
	var shouldAmount = document.getElementById(amount + index).value;
	
	var url = "${pageContext.request.contextPath}/getBasicCost.action?measureUnitName=" + measureUnitId + "&rowIndex=" + index + "&cost=" + shouldAmount;
	http.open("POST", url, true);
	http.onreadystatechange = handleHttpResponseOfBasicAmount;
	http.send(null);
	return;

}

/* 处理返回换算率后，相应基本单位数量的值处理的回调函数 */
function handleHttpResponseOfBasicAmount() {

	if (http.readyState == 4) {
		if (http.status == 200) {
			var text_array = http.responseText.split(",");
			
			document.getElementById(bAmount + text_array[0]).value = text_array[1];	
					
		} else {
			alert("error!!!");
			alert(http.status);
		}
	}
}

/* 销售报价--单位数量的转换(数量（从）到基本数量（从）的转换) */
function getBaseUnitNoStartInfo(index){
		
			var measureUnitName = document.getElementById('measureUnitName'+index).value;		
			var amountStart = document.getElementById('amountStart'+index).value;		
			
			var url = "${pageContext.request.contextPath}/getBaseUnitNoStart.action?measureUnitName="+measureUnitName+"&rowIndex="+index+"&amountStart="+amountStart;
			
			http.open("POST", url, true);
			http.onreadystatechange = handleHttpResponseOfBaseUnitNoStart;
			http.send(null);
			return;
}

function handleHttpResponseOfBaseUnitNoStart() {
	if (http.readyState == 4) {
		if (http.status == 200) {
			var text_array = http.responseText.split(",");
			//alert(text_array[0]);
			document.getElementById("baseUnitNoStart" + text_array[0]).value = text_array[1];	
					
		} else {
			alert("error!!!");
			alert(http.status);
		}
	}
}
/* 销售报价--单位数量的转换(数量（到）到基本数量（到）的转换) */
function getBaseUnitNoEndInfo(index){
		
			var measureUnitName = document.getElementById('measureUnitName'+index).value;		
			var amountEnd = document.getElementById('amountEnd'+index).value;		
			
			var url = "${pageContext.request.contextPath}/getBaseUnitNoEnd.action?measureUnitName="+measureUnitName+"&rowIndex="+index+"&amountEnd="+amountEnd;
			
			http.open("POST", url, true);
			http.onreadystatechange = handleHttpResponseOfBaseUnitNoEnd;
			http.send(null);
			return;
}

function handleHttpResponseOfBaseUnitNoEnd() {
	if (http.readyState == 4) {
		if (http.status == 200) {
			var text_array = http.responseText.split(",");
			//alert(text_array[0]);
			document.getElementById("baseUnitNoEnd" + text_array[0]).value = text_array[1];	
					
		} else {
			alert("error!!!");
			alert(http.status);
		}
	}
}
